<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowd');

class Template {

    var $data = array();
    var $default = array();
    var $main = 'default/main';
    var $js = array(
        'jquery-1.6.4.min',
        'jquery.form',
        'jquery.validate.min',
        'object'
    );
    var $css = array('main');
    var $js_globals = array(
        'BASE_URL' => BASE_URL,
        'delete_alert' => "There is no way to retrieve this data! are you sure you want to continue?"
    );
    
    var $ckeditor = false;
    var $file_upload = false;
    var $jquery_ui = false;
    var $fancybox = false;
    var $js_exeption = array();
    var $css_exeption = array();

    function __construct($par=array()) {
        if ($par && count($par) > 0) {
            $this->initialize($par);
        }
    }

    function initialize($par) {
        foreach ($par as $i => $v) {
            ($i == 'main') ? $this->main = $v : $this->data[$i] = $v;
        }
    }

    function init_param($index, $values) {
        $this->$index = (is_array($values)) ? array_merge($this->$index, $values) : array_merge($this->$index, array($values));
    }

    function get($index) {
        return $this->$index;
    }
    
    function set($index,$value,$add=false){
        
        $this->$index = ($add)?array_merge($this->$index, $value): $value;
    }

    function render() {

        $CI = & get_instance();
        
        foreach (array('js', 'css', 'js_variables','alerts','main_menu') as $v) {
            $this->collect($v);
        }
        $this->data['users_name'] = (isset ($CI->u->name))?$CI->u->name:'';
        return $CI->load->view($this->main, $this->data, TRUE);
    }

    private function collect($index) {
        $CI = & get_instance();
        $res = '';
        switch ($index) {
            case 'js':
                foreach ($this->js as $v) {
                    if(!in_array($v, $this->js_exeption )){
                        $src = JS_PATH . $v ;
                        $res .= js($src). "\n";
                    }
                }
                
                if($this->ckeditor){
                    $res .= js(IMPORTS_URL.'ckeditor/_samples/sample')."\n";
                    $res .= js(IMPORTS_URL.'ckeditor/ckeditor')."\n";
                }
                if($this->file_upload){
                    $res .= js(IMPORTS_URL.'ifunk/vendor/swfupload/swfupload')."\n";
                    $res .= js(IMPORTS_URL.'ifunk/src/jquery.swfupload')."\n";
                }
                if($this->jquery_ui){
                    $res .= js(IMPORTS_URL.'js/ui/js/jquery-ui-1.8.16.custom.min')."\n";
                }
                if($this->fancybox){
                    $res .=js(IMPORTS_URL.'js/jquery.fancybox/fancybox/jquery.mousewheel-3.0.4.pack')."\n";
                    $res .=js(IMPORTS_URL.'js/jquery.fancybox/fancybox/jquery.fancybox-1.3.4.pack')."\n";
                }
                
                break;
            case 'css':
                foreach ($this->css as $v) {
                    if(!in_array($v, $this->css_exeption )){
                        $href =  CSS_PATH . $v;
                        $res .= css($href). "\n";
                    }
                }
                if($this->ckeditor){
                    $res .= css(IMPORTS_URL.'ckeditor/_samples/sample')."\n";
                }
                if($this->jquery_ui){
                    $res .= css(IMPORTS_URL.'js/ui/css/ui-lightness/jquery-ui-1.8.16.custom')."\n";
                }
                if($this->fancybox){
                    $res .= css(IMPORTS_URL.'js/jquery.fancybox/fancybox/jquery.fancybox-1.3.4')."\n";
                }
                
                break;
            case 'js_variables';
                $res .= $this->js_variables();
                break;
            case 'alerts':
                $param = array('message'=>'','type'=>'hide');
                $alert = $CI->session->userdata('alerts');
                $this->data['show_alerts']=false;
                if($alert){$param = $alert;$this->data['show_alerts']=true;}
                $res = $CI->load->view('crud/alerts',$param,TRUE);
                $CI->session->unset_userdata('alerts');
                break;
            case 'main_menu':
                $res = $CI->load->view('default/main_menu',array('controller' => $this->data['controller']),TRUE);
                break;    
            default :
                break;
        }

        $this->data[$index] = $res;
    }

    function js_variables() {
        $res = '';
        foreach ($this->js_globals as $i => $v) {
            $res .= "\n var " . $i . " = '" . $v . "';";
        }

        return '<script type="text/javascript"> ' . $res . '</script>' . "\n";
    }
}
